function [AggressiveOrders, RegMtrx,Y] = MicroStructureES(Data)

%Takes in Data formated as [OriginalTime BidPrice BidSize OfferPrice OfferSize TradePrice TradeSize]
[n,m] = size(Data);
Data = [Data];
AggressiveOrders = zeros(round(n/2),12);

%Aggressive orders has time, price, size, aggressor side, grouped
%This routine cycles through the whole data and aggregates consecutive
%transaction records into one large originating aggressive order; it also
%captures the bid/offer dynamics of the order book at the time of the order

minFluc = 1;
aggressivecount = 0;
aggressivegrouptotal = 0;
aggressivestartprice = 0;
aggressivestarttime = 0;
aggresesorside = 0;
bid = 2000;
bidSize = 100;
offerSize = 100;
offer = 2000;
inGroup = 0;
counter = 0;
nextBidFlag= 0;
nextOfferFlag = 0;
while(counter<n)
    counter = counter + 1;
    if(~isnan(Data(counter,7)) && ~inGroup)
        inGroup = 1;
        nextOfferFlag = 0;
        nextBidFlag = 0;
        aggressivegrouptotal = Data(counter,7);
        aggressivestartprice = Data(counter,6);
        aggressivestarttime = Data(counter,1);
        if(abs(Data(counter,6) - offer)<0.0000001 )
            aggresesorside = 1;
        else
            aggresesorside = -1;
        end
    else
        if(~isnan(Data(counter,7)) && inGroup)
            aggressivegrouptotal = aggressivegrouptotal+ Data(counter,7);
        else
            if(inGroup)
                inGroup = 0;
                aggressivecount = aggressivecount + 1;
                AggressiveOrders(aggressivecount,:) = [aggressivestarttime aggressivestartprice aggressivegrouptotal aggresesorside (bid+offer)/2 (offer*bidSize+bid*offerSize)/(bidSize + offerSize) 0 0 0 0 bidSize offerSize];
                if(aggresesorside>0)
                    AggressiveOrders(aggressivecount,7:10) = [bid bidSize offer offerSize];
                    nextOfferFlag = 1;
                else
                    AggressiveOrders(aggressivecount,7:10) = [bid bidSize offer offerSize];
                    nextBidFlag = 1;
                end
                %Perhaps change to reflect updates to bid and offer that
                %come next: mechanical depletion component in one column
                %and true price next (differential between true price pre
                %and post)
            end
        end
    end
    if(~isnan(Data(counter,2)) || ~isnan(Data(counter,4)))
        if(~isnan(Data(counter,2)))
            bid = Data(counter,2);
            bidSize = Data(counter,3);
            if(nextBidFlag == 1)
                AggressiveOrders(aggressivecount,7:8) = [bid bidSize];
                nextBidFlag = 0;
            else
                if(nextOfferFlag == 1 && isnan(Data(counter,4)))
                    nextOfferFlag = 0;
                end
            end
        end
        if(~isnan(Data(counter,4)))
            offer = Data(counter,4);
            offerSize = Data(counter,5);
            if(nextOfferFlag == 1)
                AggressiveOrders(aggressivecount,9:10) = [offer offerSize];
                nextOfferFlag = 0;
            else
                if(nextBidFlag == 1 && isnan(Data(counter,2)))
                    nextBidFlag = 0;
                end
            end
        end 
    end
end

AggressiveOrders = AggressiveOrders(1:aggressivecount,:);
[n1,m1] = size(AggressiveOrders);


RegMtrx = zeros(aggressivecount - 100,10);
Y = zeros(aggressivecount - 100,1);
aggressivecount = 0;
regCounter = 0;
aggressivegrouptotal = 0;
aggressivestartprice = 0;
aggressivestarttime = 0;
aggresesorside = 0;
lockedBidDepth = 100;
lockedOfferDepth = 100;
buyDepthChange = 0;
sellDepthChange = 0;

lockedqBarDCB = 0;
lockedqBarDCS = 0;
lockedQFlag = 0;
spreadflag = 1;
totalDCQ = 0;
bid = 2000;
bidSize = 1;
offer = 2000;
offerSize = 1;
inGroup = 0;
lastPrice = 0;
counter = 0;
while(counter<n)
    counter = counter + 1;
    if(~isnan(Data(counter,7)) && ~inGroup)
        inGroup = 1;
        if(lockedQFlag == 1)
            lockedqBarDCB = bidSize - lockedQBidDepth;
            lockedqBarDCS = lockedQOfferDepth - offerSize;
            totalDCQ = lockedqBarDCB + lockedqBarDCS;
            lockedQFlag = 0;
        end
        lastPrice = Data(counter,6);
        buyDepthChange = bidSize - lockedBidDepth;
        sellDepthChange = lockedOfferDepth - offerSize;
        if(abs(Data(counter,6) - offer)<0.0000001 )
            aggresesorside = 1;
        else
            aggresesorside = -1;
        end
    else
        if(~isnan(Data(counter,7)) && inGroup)
            lastPrice = Data(counter,6);
        else
            %Exiting the aggressive order
            if(inGroup)
                inGroup = 0;
                aggressivecount = aggressivecount + 1;
                if(aggressivecount>50 && aggressivecount<n1-51)
                    regCounter = regCounter + 1;
                    Y(regCounter,1) = AggressiveOrders(aggressivecount+1,6)-AggressiveOrders(aggressivecount,6);
                end
                spreadflag = 1;
                if(AggressiveOrders(aggressivecount,4) == 1)
                    if(lastPrice>offer)
                        if(~isnan(Data(counter,4)))
                            if(abs(offer-Data(counter,4))>0.0000001)
                                spreadflag = -1;
                            end
                            offer = Data(counter,4);
                            offerSize = Data(counter,5);
                            lockedOfferDepth = offerSize;
                        else
                            lockedOfferDepth = NaN;
                        end
                        if(~isnan(Data(counter,2)))
                            if(abs(bid-Data(counter,2))>0.0000001)
                                spreadflag = -1;
                            end
                            bid = Data(counter,2);
                            bidSize = Data(counter,3);
                            lockedBidDepth = bidSize;
                        end
                    else
                        lockedBidDepth = bidSize;
                        lockedOfferDepth = offerSize - AggressiveOrders(aggressivecount,3);
                        if(AggressiveOrders(aggressivecount,3)<= 5)
                            lockedQBidDepth = lockedBidDepth;
                            lockedQOfferDepth = lockedOfferDepth;
                            lockedQFlag = 1;
                        end
                    end
                end
                if(AggressiveOrders(aggressivecount,4) == -1)
                    if(lastPrice<bid)
                        if(~isnan(Data(counter,2)))
                            if(abs(bid-Data(counter,2))>0.0000001)
                                spreadflag = -1;
                            end
                            bid = Data(counter,2);
                            bidSize = Data(counter,3);
                            lockedBidDepth = bidSize;
                        else
                            lockedBidDepth = NaN;
                        end
                        if(~isnan(Data(counter,4)))
                            if(abs(offer-Data(counter,4))>0.0000001)
                                spreadflag = -1;
                            end
                            offer = Data(counter,4);
                            offerSize = Data(counter,5);
                            lockedOfferDepth = offerSize;
                        end
                    else
                        lockedOfferDepth = offerSize;
                        lockedBidDepth = bidSize - AggressiveOrders(aggressivecount,3);
                        if(AggressiveOrders(aggressivecount,3)<= 5)
                            lockedQBidDepth = lockedBidDepth;
                            lockedQOfferDepth = lockedOfferDepth;
                            lockedQFlag = 1;
                        end
                    end
                end       
            end
            %It's not a trade datapoint, it's bid or offer
            if(~isnan(Data(counter,2)) || ~isnan(Data(counter,4)))
                if(~isnan(Data(counter,2)))
                    if(abs(bid-Data(counter,2))>0.0000001)
                        spreadflag = -1;
                    end
                    bid = Data(counter,2);
                    bidSize = Data(counter,3);
                end
                if(~isnan(Data(counter,4)))
                    if(abs(offer-Data(counter,4))>0.0000001)
                        spreadflag = -1;
                    end
                    offer = Data(counter,4);
                    offerSize = Data(counter,5);
                end 
                if(abs((offer-bid)-minFluc)>0.0000001)
                    spreadflag = -1;
                end
            end
        end
    end
    
end



end